package com.greentravel.service.impl;

import com.greentravel.dao.TravelRecordDAO;
import com.greentravel.dao.impl.TravelRecordDAOImpl;
import com.greentravel.model.TravelRecord;
import com.greentravel.service.TravelRecordService;
import com.greentravel.service.UserService;
import com.greentravel.util.CarbonCalculator;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 出行记录服务实现类
 * 实现与出行记录相关的具体业务逻辑
 */
public class TravelRecordServiceImpl implements TravelRecordService {

    private TravelRecordDAO travelRecordDAO;
    private UserService userService;

    public TravelRecordServiceImpl() {
        travelRecordDAO = new TravelRecordDAOImpl();
        userService = new UserServiceImpl();
    }

    @Override
    public boolean addTravelRecord(int userId, Date travelTime, String travelMode, String startLocation,
            String endLocation, double distance) {
        // 计算碳减排量和积分
        double carbonReduction = CarbonCalculator.calculateCarbonReduction(travelMode, distance);
        int points = CarbonCalculator.calculatePoints(travelMode, distance);

        // 创建出行记录对象
        TravelRecord record = new TravelRecord();
        record.setUserId(userId);
        record.setTravelTime(travelTime);
        record.setTravelMode(travelMode);
        record.setStartLocation(startLocation);
        record.setEndLocation(endLocation);
        record.setDistance(distance);
        record.setCarbonReduction(carbonReduction);
        record.setPoints(points);

        // 添加记录到数据库
        boolean result = travelRecordDAO.addTravelRecord(record);

        // 如果添加成功，给用户增加积分
        if (result) {
            userService.addUserPoints(userId, points);
        }

        return result;
    }

    @Override
    public List<TravelRecord> getUserTravelRecords(int userId) {
        return travelRecordDAO.getTravelRecordsByUserId(userId);
    }

    @Override
    public List<TravelRecord> getUserTravelRecordsByDateRange(int userId, Date startDate, Date endDate) {
        return travelRecordDAO.getTravelRecordsByDateRange(userId, startDate, endDate);
    }

    @Override
    public List<TravelRecord> getUserTravelRecordsByMode(int userId, String travelMode) {
        return travelRecordDAO.getTravelRecordsByTravelMode(userId, travelMode);
    }

    @Override
    public double getTotalDistance(int userId) {
        return travelRecordDAO.getTotalDistanceByUserId(userId);
    }

    @Override
    public double getTotalCarbonReduction(int userId) {
        return travelRecordDAO.getTotalCarbonReductionByUserId(userId);
    }

    @Override
    public Map<String, Integer> getTravelModeStatistics(int userId) {
        return travelRecordDAO.getTravelModeStatistics(userId);
    }

    @Override
    public double getAverageDistance(int userId) {
        return travelRecordDAO.getAverageDistanceByUserId(userId);
    }

    @Override
    public boolean deleteTravelRecord(int recordId) {
        // 首先获取记录，以便删除后扣除用户的积分
        TravelRecord record = travelRecordDAO.getTravelRecordById(recordId);
        if (record == null) {
            return false;
        }

        boolean result = travelRecordDAO.deleteTravelRecord(recordId);

        // 如果删除成功，从用户积分中扣除对应的积分
        if (result) {
            userService.addUserPoints(record.getUserId(), -record.getPoints());
        }

        return result;
    }

    @Override
    public boolean updateTravelRecord(TravelRecord record) {
        // 获取原记录，计算积分差值
        TravelRecord oldRecord = travelRecordDAO.getTravelRecordById(record.getId());
        if (oldRecord == null) {
            return false;
        }

        // 重新计算碳减排量和积分
        double carbonReduction = CarbonCalculator.calculateCarbonReduction(record.getTravelMode(),
                record.getDistance());
        int points = CarbonCalculator.calculatePoints(record.getTravelMode(), record.getDistance());
        record.setCarbonReduction(carbonReduction);
        record.setPoints(points);

        boolean result = travelRecordDAO.updateTravelRecord(record);

        // 如果更新成功，调整用户积分
        if (result) {
            // 计算积分差值
            int pointsDiff = points - oldRecord.getPoints();
            if (pointsDiff != 0) {
                userService.addUserPoints(record.getUserId(), pointsDiff);
            }
        }

        return result;
    }
}